<?php 
add_filter( 'show_admin_bar', '__return_false' );



/*******************************
TWEET COUNT
********************************/

function getTweetCount($url)
{
    $url = urlencode($url);
    $twitterEndpoint = "http://urls.api.twitter.com/1/urls/count.json?url=%s";
    $fileData = file_get_contents(sprintf($twitterEndpoint, $url));
    $json = json_decode($fileData, true);
    unset($fileData); // free memory
    //print_r($json);
    return $json['count'];
}
/*******************************
 WIDGETS AREAS
********************************/
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h2>',
        'after_title' => '</h2>',
    ));
	
/*******************************
 MENUS SUPPORT
********************************/
if ( function_exists( 'wp_nav_menu' ) ){
	if (function_exists('add_theme_support')) {
		add_theme_support('nav-menus');
		add_action( 'init', 'register_my_menus' );
		function register_my_menus() {
			register_nav_menus(
				array(
					'top-links' => __( 'Top Links' ),
					'main-menu' => __( 'Main Menu' ),
					'footer-links' => __( 'Footer Links' )
				)
			);
		}
	}
}

/* CallBack functions for menus in case of earlier than 3.0 Wordpress version or if no menu is set yet*/

function toplinks(){ ?>
		<div id="topMenu">
			<ul>
				<li><a href="<?php bloginfo('url'); ?>/">Home</a></li>
				<?php wp_list_pages('title_li=') ?>
			</ul>
		</div>
<?php }

function footerlinks(){ ?>
		<div id="footerMenu">
			<ul>
					<li><a href="<?php bloginfo('url'); ?>/">Home</a></li>
					<?php wp_list_pages('title_li=') ?>
			</ul>
		</div>
<?php }

function mainmenu(){ ?>
		<div id="mainMenu">
				<ul class="sf-menu">
				<?php wp_list_categories('hide_empty=1&exclude=1&title_li='); ?>
				</ul>
		</div>
<?php }

/*******************************
 CUSTOM COMMENTS
********************************/

function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
  	

     <div id="comment-<?php comment_ID(); ?>" >
	 
	 <div class="comment-metaLeft">
			<?php echo get_avatar($comment,$size='40',$default='http://www.gravatar.com/avatar/61a58ec1c1fba116f8424035089b7c71' ); ?>
			</div>
			<div class="commentRight">
			<div class="commentBullet"></div>
				 <span><?php printf(__('<strong>%s</strong>'), get_comment_author_link()) ?> </span><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?> <?php edit_comment_link(__('(Edit)'),'  ','') ?>
				
				  <div class="text"><?php comment_text() ?> </div>
				   <?php if ($comment->comment_approved == '0') : ?>
					 <em><?php _e('Your comment is awaiting moderation.') ?></em>
				  <?php endif; ?>
			<div class="reply">
					 <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
				  </div>
			</div>
	 
      
     </div>
<?php }

/*******************************
  THEME OPTIONS PAGE
********************************/
add_action('admin_menu', 'colorbold_theme_page');
function colorbold_theme_page ()
{
	if ( count($_POST) > 0 && isset($_POST['colorbold_settings']) )
	{
		$options = array ( 'style','logo_img','logo_alt','contact_email','ads', 'twitter_link', 'twitter_txt','flickr', 'analytics');
		
		foreach ( $options as $opt )
		{
			delete_option ( 'colorbold_'.$opt, $_POST[$opt] );
			add_option ( 'colorbold_'.$opt, $_POST[$opt] );	
		}	
		header("Location: admin.php?page=functions.php&saved=true");								
			
		die;		
	}
	add_theme_page(__('Colorbold Options'), __('Colorbold Options'), 'edit_themes', basename(__FILE__), 'colorbold_settings');	
}
function colorbold_settings ()
{?>
<div class="wrap">
	<h2>Colorbold Options Panel</h2>
	
<form method="post" action="">
	<table class="form-table">
		<!-- General settings -->	
		<tr valign="top">
			<th scope="row"><label for="style">Theme Color Scheme</label></th>
			<td>
				<select name="style" id="style">
					<option value="blue.css" <?php if(get_option('colorbold_style') == 'blue.css'){?>selected="selected"<?php }?>>blue.css</option>
					<option value="magenta.css" <?php if(get_option('colorbold_style') == 'magenta.css'){?>selected="selected"<?php }?>>magenta.css</option>
					<option value="green.css" <?php if(get_option('colorbold_style') == 'green.css'){?>selected="selected"<?php }?>>green.css</option>
					<option value="red.css" <?php if(get_option('colorbold_style') == 'red.css'){?>selected="selected"<?php }?>>red.css</option>
					
				</select> 
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="logo_img">Change logo (full path to image)</label></th>
			<td>
				<input name="logo_img" type="text" id="logo_img" value="<?php echo get_option('colorbold_logo_img'); ?>" class="regular-text" /><br />
				<em>current logo:</em> <br /> <img src="<?php echo get_option('colorbold_logo_img'); ?>" alt="<?php bloginfo('name'); ?>" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="logo_alt">Logo image ALT text</label></th>
			<td>
				<input name="logo_alt" type="text" id="logo_alt" value="<?php echo get_option('colorbold_logo_alt'); ?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="contact_email">Email Address for Contact Form</label></th>
			<td>
				<input name="contact_email" type="text" id="contact_email" value="<?php echo get_option('colorbold_contact_email'); ?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="twitter_link">Twitter link</label></th>
			<td>
				<input name="twitter_link" type="text" id="twitter_link" value="<?php echo get_option('colorbold_twitter_link'); ?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="twitter_txt">Twitter box text</label></th>
			<td>
				<input name="twitter_txt" type="text" id="twitter_txt" value="<?php echo get_option('colorbold_twitter_txt'); ?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="flickr">Flickr Photostream</label></th>
			<td>
				<select name="flickr" id="flickr">
					<option value="no" <?php if(get_option('colorbold_flickr') == 'no'){?>selected="selected"<?php }?>>No</option>
					<option value="yes" <?php if(get_option('colorbold_flickr') == 'yes'){?>selected="selected"<?php }?>>Yes</option>
				</select> 
				<br /><em>Make sure you have FlickrRSS plugin activated if you choose to enable Flickr Photostream</em>
			</td>
		</tr>
		<!-- Ads Box Settings -->
		<tr>
			<th><label for="ads">Ads Section Enabled:</label></th>
			<td>
				<select name="ads" id="ads">
					<option value="no" <?php if(get_option('colorbold_ads') == 'no'){?>selected="selected"<?php }?>>No</option>
					<option value="yes" <?php if(get_option('colorbold_ads') == 'yes'){?>selected="selected"<?php }?>>Yes</option>
				</select> 
				<br /><em>Make sure you have AdMinister plugin activated and have the position "Sidebar" created within the plugin.</em>
			</td>
		</tr>
		<!-- Google Analytics -->
		<tr>
			<th><label for="ads">Google Analytics code:</label></th>
			<td>
				<textarea name="analytics" id="analytics" rows="7" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('colorbold_analytics')); ?></textarea>
			</td>
		</tr>
		
		
	</table>
	<p class="submit">
		<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
		<input type="hidden" name="colorbold_settings" value="save" style="display:none;" />
	</p>
</form>

</div>
<?php }

function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
} 
/*******************************
  CONTACT FORM 
********************************/

 function hexstr($hexstr) {
  $hexstr = str_replace(' ', '', $hexstr);
  $hexstr = str_replace('\x', '', $hexstr);
  $retstr = pack('H*', $hexstr);
  return $retstr;
}

function strhex($string) {
  $hexstr = unpack('H*', $string);
  return array_shift($hexstr);
}


function kriesi_pagination($pages = '', $range = 2)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class='pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
}

?>